<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "store".
 *
 * @property int $id
 * @property string $name 店铺名称
 * @property int $region_1 地域1
 * @property int $region_2 地域2
 * @property int $region_3 地域3
 * @property string $address 地址
 * @property string $bussiness_no 营业执照号码
 * @property string $bussiness_img 营业执照照片
 * @property int $userid 用户ID
 * @property string $username 用户名
 * @property int $type 入驻类型 1区域代理2商家
 * @property string $liaison 联系人
 * @property int $liaison_id_type 联系人证件类型 1身份证2护照3港澳通行证4军官证
 * @property int $liaison_id_no 联系人证件号码
 * @property string $liaison_id_fimg 证件正面照片
 * @property string $liaison_id_bimg 证件背面照片
 * @property string $liaison_mobile 联系人手机
 * @property string $liaison_email 联系人邮箱
 * @property string $liaison_job 联系人职务
 * @property string $shop_info 店铺信息
 * @property string $shop_logo 店铺logo
 * @property int $status 0待审核1已通过2已拒绝
 * @property string $reason 未通过原因
 * @property int $addtime 添加时间
 */
class Store extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'store';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['region_1', 'region_2', 'region_3', 'address', 'userid', 'username', 'liaison', 'liaison_id_type', 'liaison_id_no', 'liaison_mobile', 'shop_info', 'addtime'], 'required'],
            [['region_1', 'region_2', 'region_3', 'userid', 'addtime'], 'integer'],
            [['shop_info'], 'string'],
            [['name', 'username', 'reason'], 'string', 'max' => 150],
            [['address'], 'string', 'max' => 300],
            [['bussiness_no'], 'string', 'max' => 100],
            [['bussiness_img', 'liaison_id_fimg', 'liaison_id_bimg', 'shop_logo'], 'string', 'max' => 120],
            [['type', 'liaison_id_type', 'status'], 'string', 'max' => 2],
            [['liaison'], 'string', 'max' => 80],
            [['liaison_mobile'], 'string', 'max' => 20],
            [['liaison_email'], 'string', 'max' => 60],
            [['liaison_job'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '店铺名称',
            'region_1' => '地域1',
            'region_2' => '地域2',
            'region_3' => '地域3',
            'address' => '地址',
            'bussiness_no' => '营业执照号码',
            'bussiness_img' => '营业执照照片',
            'userid' => '用户ID',
            'username' => '用户名',
            'type' => '入驻类型 1区域代理2商家',
            'liaison' => '联系人',
            'liaison_id_type' => '联系人证件类型 1身份证2护照3港澳通行证4军官证',
            'liaison_id_no' => '联系人证件号码',
            'liaison_id_fimg' => '证件正面照片',
            'liaison_id_bimg' => '证件背面照片',
            'liaison_mobile' => '联系人手机',
            'liaison_email' => '联系人邮箱',
            'liaison_job' => '联系人职务',
            'shop_info' => '店铺信息',
            'shop_logo' => '店铺logo',
            'status' => '0待审核1已通过2已拒绝',
            'reason' => '未通过原因',
            'addtime' => '添加时间',
        ];
    }
    
    
    public static function applysave($userid,$username,$param){
        $model = new Store();
        $model->name = $param['name'];
        $model->region_1 = $param['region_1'];
        $model->region_2 = $param['region_2'];
        $model->region_3 = $param['region_3'];
        $model->address = $param['address'];
        $model->bussiness_no = $param['bussiness_no'];
        $model->bussiness_img = $param['bussiness_img'];
        $model->userid = $userid;
        $model->username = $username;
        $model->type = $param['type'];
        $model->liaison = $param['liaison'];
        $model->liaison_id_type = $param['liaison_id_type'];
        $model->liaison_id_no = $param['liaison_id_no'];
        $model->liaison_mobile = $param['liaison_mobile'];
        $model->liaison_email = $param['liaison_email'];
        $model->liaison_job = $param['liaison_job'];
        $model->shop_info = $param['shop_info'];
        $model->shop_logo = $param['shop_logo'];
        $model->addtime = time();
        if($model->save()){
            return true;
        }else{
            
            $output['success'] = 0;
                $output['msg'] = $model->errors;
                $output['errocode'] = 1001;
	        Tools::outputjson($output);
            //return $model->errors;
        }
    }
    
    
    public static function getStoreByUserid($userid){
        $sql = "select * from store where userid=$userid";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->queryOne();
    }
    
    public static function getBackendStoreList($condition,$page_no){
        //获取符合条件的商品的个数
        
        $perpage = 15;
        $sql_condition_arr = [];
        array_push($sql_condition_arr,"1=1");
        if(isset($condition['name']) && $condition['name'] !== ""){
            array_push($sql_condition_arr,"name='".$condition['name']."'");
        }
        if(isset($condition['type']) && $condition['type'] !== 0){
            array_push($sql_condition_arr,"type='".$condition['type']."'");
        }
        if(isset($condition['status']) && $condition['status'] !== ""){
            array_push($sql_condition_arr,"status=".$condition['status']."");
        }
        if(isset($condition['userid']) && $condition['userid'] !== ""){
            array_push($sql_condition_arr,"userid=".$condition['userid']);
        }
        
        $sql_condition_string = implode(' and ', $sql_condition_arr);
        $data['total'] = self::getCountByCondition($sql_condition_string);
        $begin = $perpage*($page_no-1);
        $storedata = self::getStoreByPage($sql_condition_string, $begin, $perpage);
        foreach($storedata as $key=>$value){
            $storedata[$key]['province'] = Region::getRegionNameById($value['region_1']);
            $storedata[$key]['city'] = Region::getRegionNameById($value['region_2']);
            $storedata[$key]['county'] = Region::getRegionNameById($value['region_3']);
            $storedata[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
        }
        $data['storedata'] = $storedata;
        $data['pagenums'] = ceil($data['total']/$perpage);
        $data['perpage'] = $perpage;
        return $data;
    }
    
    public static function getCountByCondition($sql_condition_string){
        $sql = "select count(id) total from store where $sql_condition_string";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryOne();
        return $model['total'];
    }
    
    public static function getStoreByPage($sql_condition_string,$begin,$perpage){
        $sql = "select * from store where $sql_condition_string order by id desc limit $begin,$perpage";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
}
